import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
import requests
import weipanui
import time
from time import *
import pandas as pd

ID = "ww7b4086d88a144eec"
SECRET = "QqZ9Au1EwEDc1PyvcZaAgJBhaPDeK1z5zinENKHLL1Q"
url_get_access_token = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + ID + "&corpsecret=" + SECRET
def get_access_token(ID,SECRET):
    access_token_json = requests.get(url_get_access_token).json()
    while access_token_json['errcode'] != 0:
        get_access_token(ID, SECRET)
        time.sleep(1)
    access_token = access_token_json['access_token']
    return access_token

url_get_moment_list = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_moment_list?access_token=" + get_access_token(ID,SECRET)
def get_moment_list(start_time, end_time, type):
    i = 1
    next_cursor = ''
    moment_detail_list = []
    while i == 1 or next_cursor != '':
        moment_publish_inform = {
            "start_time": start_time,
            "end_time": end_time,
            "filter_type": type,
            "cursor": next_cursor,
            "limit": 100
        }
        moment_publish = requests.post(url_get_moment_list, json=moment_publish_inform)
        moment_list = moment_publish.json()['moment_list'] #moment_list是个列表
        for moment in moment_list:
            moment_id = moment['moment_id']
            creator = moment['creator']
            create_type = moment['create_type']
            visible_type = moment['visible_type']
            try:
                text = moment['text']['content'][:50]
            except:
                text = "/"
            try:
                media_id = moment['video']['media_id']
                video = "是"
            except:
                video = "否"
            moment_detail = [moment_id, creator, create_type, visible_type, text, video]
            moment_detail_list.append(moment_detail)
        next_cursor = moment_publish.json()['next_cursor']
        i = i + 1
    moment_detail_list_df = pd.DataFrame(list(moment_detail_list),
                                  columns=['朋友圈id', '员工工号', '群发方式', '可见方式', '文案(前50字)', '有视频'])
    with pd.ExcelWriter('D:\企微朋友圈发表记录%s.xlsx' % (strftime('%Y%m%d%H%M%S', localtime(time())))) as writer:
        moment_detail_list_df.to_excel(writer, engine='xlsxwriter', sheet_name='企微朋友圈发表记录')
    return

if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = weipanui.Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()

    def change_start_time():
        start_time = ui.dateTimeEdit.dateTime()
        #ui.dateTimeEdit.dateTime = start_time
        return start_time
    ui.dateTimeEdit.dateTimeChanged.connect(change_start_time)
    def change_end_time():
        end_time = ui.dateTimeEdit_2.dateTime()
        #ui.dateTimeEdit_2.dateTime = end_time
        return end_time
    ui.dateTimeEdit_2.dateTimeChanged.connect(change_end_time)

    def pushButton_clicked():
        start_time = ui.dateTimeEdit.dateTime().toTime_t()
        end_time = ui.dateTimeEdit_2.dateTime().toTime_t()
        get_moment_list(start_time, end_time, 1)
        ui.label_4.setText("导出完成,在D盘查看")
        return
    ui.pushButton.clicked.connect(pushButton_clicked)
    sys.exit(app.exec_())